﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Net;

namespace HttpMultiThreadDownloadExample
{
    class HttpDownload
    {
        private ListBox listbox;
        private int threadIndex;

        private Stopwatch stopwatch = new Stopwatch();
        
        private int StartPosition { get; set; }

        public string TaretFileName { get; set; }

        public string Url { get; set; }

        public int FileSize { get; set; }

        public bool IsFinish { get; set; }

        public HttpDownload(ListBox l, int t)
        {
            listbox = l;
            threadIndex = t;
        }

        public void Receive()
        {
            stopwatch.Restart();
            AddStatus("线程："+threadIndex+"开始");
            int totalBytes = 0;
            using(FileStream fs = new FileStream(TaretFileName,FileMode.Create))
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                    request.AddRange(StartPosition, StartPosition + FileSize - 1);
                    Stream stream = request.GetResponse().GetResponseStream();
                    byte[] receiveBytes = new byte[512];

                    int rbytes = stream.Read(receiveBytes, 0, receiveBytes.Length);
                    while(rbytes>0)
                    {
                        fs.Write(receiveBytes, 0, rbytes);
                        totalBytes += rbytes;
                        rbytes = stream.Read(receiveBytes, 0, receiveBytes.Length);
                    }
                    stream.Close();
                        
                }
                catch (System.Exception ex)
                {
                    AddStatus("线程" + threadIndex + "出错" + ex.Message);
                }
            }

            stopwatch.Stop();
            IsFinish = true;
            ChangeStatus("线程：" + threadIndex + "开始", "接收完毕", totalBytes);
            

        }

        private delegate void ChangeStatusDelegate(string oldmes, string newmes, int num);
        private void ChangeStatus(string oldmes, string newmes, int num)
        {
            if (listbox.InvokeRequired)
            {
                ChangeStatusDelegate d = ChangeStatus;
                listbox.Invoke(d, oldmes, newmes, num);
            }
            else
            {
                int i = listbox.FindString(oldmes);
                if (i != -1)
                {
                    string[] items = new string[listbox.Items.Count];
                    listbox.Items.CopyTo(items, 0);
                    items[i] = oldmes + " " + newmes + " 接收字节：" + Math.Ceiling(num/1024.0f) + "KB" + "用时：" + stopwatch.ElapsedMilliseconds / 1000.0f + "秒";
                    listbox.Items.Clear();
                    listbox.Items.AddRange(items);
                    listbox.SelectedIndex = i;
                }
            }
        }

        private delegate void AddStatusDelegete(string status);
        private void AddStatus(string status)
        {
            if (listbox.InvokeRequired)
            {
                AddStatusDelegete d = AddStatus;
                listbox.Invoke(d, status);
            }
            else
            {
                listbox.Items.Add(status);
            }
        }
    }
}
